<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="账密设置"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" :model="form" :rules="rules">
      <el-form-item label="链接" prop="StoreUrl">
        <el-input v-model="form.StoreUrl" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理地址">
        <el-input v-model="form.ManageUrl" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理账号" prop="ManageAccount">
        <el-input v-model="form.ManageAccount" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理密码" prop="ManagePassword">
        <el-input v-model="form.ManagePassword" :disabled="disabled" />
      </el-form-item>

      <!-- <el-form-item v-if="!disabled" label="实际服务期限">
        <el-date-picker
          v-model="form.ServiceDates"
          type="daterange"
          start-placeholder="Start date"
          end-placeholder="End date"
          :default-time="defaultTime"
          :disabled="disabled"
        />
      </el-form-item> -->
      <el-form-item v-if="form.isShowPassword" label="查看密码">
        <el-input v-model="form.ShowPassword" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="!disabled" type="primary" @click="handleSaveShopLink">
          提交保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
  import { showStore } from '@/api/store'
  import { saveShopLink } from '@/api/businessOrder'
  export default defineComponent({
    name: 'StoreLink',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        disabled: false,
        formRef: null,
        form: {},
        shopLinkForm: {},
        rules: {
          StoreUrl: [
            { required: true, trigger: 'blur', message: '请输入链接' },
          ],
          // ManageUrl: [
          //   { required: true, trigger: 'blur', message: '请输入管理地址' },
          // ],
          ManageAccount: [
            { required: true, trigger: 'blur', message: '请输入管理账号' },
          ],
          ManagePassword: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          ServiceDates: [
            { required: true, trigger: 'blur', message: '请选择服务期限' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        StoreOptions: [],
        BindName: '',
      })

      const showEdit = async (row, flag, disabled) => {
        state.disabled = disabled
        state.shopLinkForm = Object.assign({}, row)

        // state.form.ServiceDates = [
        //  state.shopLinkForm.PlanServiceStartDate,
        //  state.shopLinkForm.PlanServiceEndDate,
        // ]
        state.form.CustomerID = state.shopLinkForm.CustomerID
        state.form.OrderID = state.shopLinkForm.OrderID
        state.form.OrderListID = state.shopLinkForm.OrderListID
        state.form.BindStoreID = state.shopLinkForm.BindStoreID
        state.form.StoreID = state.shopLinkForm.BindID
        state.form.StoreUrl = state.shopLinkForm.StoreUrl
        if (!state.shopLinkForm.ManageAccount)
          state.form.ManageAccount = state.shopLinkForm.BindStoreAccount
        if (flag) {
          await gitshowStore()
        }
        state.dialogFormVisible = true
      }

      const gitshowStore = async () => {
        const dataMsg = {
          StoreID: state.form.StoreID,
        }
        const { data } = await showStore(dataMsg)
        state.form = data
        // state.form.ServiceDates = [
        //   state.shopLinkForm.PlanServiceStartDate,
        //   state.shopLinkForm.PlanServiceEndDate,
        // ]
        state.form.CustomerID = state.shopLinkForm.CustomerID
        state.form.OrderID = state.shopLinkForm.OrderID
        state.form.OrderListID = state.shopLinkForm.OrderListID
        state.form.BindStoreID = state.shopLinkForm.BindStoreID
        state.form.StoreID = state.shopLinkForm.BindID
        state.form.StoreUrl = state.shopLinkForm.StoreUrl
        console.log(state.form, '123')
      }
      const handleSaveShopLink = () => {
        // const { msg } = await saveShopLink(state.form)
        // $baseMessage(msg, 'success', 'vab-hey-message-success')
        // emit('fetch-data')
        // state.dialogFormVisible = false

        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveShopLink(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            state.dialogFormVisible = false
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        gitshowStore,
        handleSaveShopLink,
        close,
      }
    },
  })
</script>
